﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Actuals.CoreDomain;
using Actuals.CoreDomain.Data;


namespace Actuals.Data
{
    public class DalEmployee : Dal<IEmployee, Employee>, IDalEmployee
    {
        private readonly IDalManager _manager;
        private static readonly IDalEmployee _instance = new DalEmployee();
        private const string SP_GET_EMPLOYEES = "usp_GetEmployees";
        private const string SP_GET_EMPLOYEE = "usp_GetEmployee";
        private const string SP_ADD_EMPLOYEE = "usp_AddEmployee";
        private const string SP_UPDATE_EMPLOYEE = "usp_UpdateEmployee";
        private const string SP_DELETE_EMPLOYEE = "usp_DeleteEmployee";



        public static IDalEmployee Instance
        {
            get { return _instance; }
        }

        public DalEmployee()
        {
            _manager = new DalManager();
        }

        public DalEmployee(IDalManager manager)
        {
            _manager = manager;
        }

        protected override void Convert(IEmployee obj, DataRow row)
        {
            obj.Id = _manager.GetValue<int>(row, "Id");
            obj.FirstName = _manager.GetValue<string>(row, "FirstName");
            obj.LastName = _manager.GetValue<string>(row, "LastName");
            obj.NickName = _manager.GetValue<string>(row, "NickName");
            obj.DOB = _manager.GetValue<DateTime?>(row, "DOB");
            obj.RoleId = _manager.GetValue<int>(row, "RoleId");
        }

        #region IDalProject Members

        public IList<IEmployee> GetEmployees()
        {
            _manager.StoredProcedureName = SP_GET_EMPLOYEES;
            List<IEmployee> employees = new List<IEmployee>();
            foreach (IEmployee employee in ConvertAll(_manager.ExecuteDataSet()))
                employees.Add(employee);

            return employees;


        }

        public IEmployee GetEmployee(int employeeId)
        {
            _manager.StoredProcedureName = SP_GET_EMPLOYEE;
            _manager.AddInParameter("@EmployeeId", DbType.Int32, employeeId);


            IEmployee employee = Convert(_manager.ExecuteDataSet());

            return employee;
        }
        public void DeleteEmployee(int employeeId)
        {
            _manager.StoredProcedureName = SP_DELETE_EMPLOYEE;
            _manager.AddInParameter("@Id", DbType.Int32, employeeId);
            _manager.ExecuteNonQuery();

        }
        public void AddEmployee(IEmployee employee)
        {
            _manager.StoredProcedureName = SP_ADD_EMPLOYEE;
            _manager.AddInParameter("@FirstName", DbType.String, employee.FirstName);
            _manager.AddInParameter("@LastName", DbType.String, employee.LastName);
            _manager.AddInParameter("@NickName", DbType.String, employee.NickName);
            _manager.AddInParameter("@RoleId", DbType.Int32, employee.RoleId);
            _manager.AddInParameter("@DOB", DbType.DateTime, employee.DOB);

            _manager.ExecuteNonQuery();
       
        }
        public void UpdateEmployee(IEmployee employee)
        {
            _manager.StoredProcedureName = SP_UPDATE_EMPLOYEE;
            _manager.AddInParameter("@Id", DbType.String, employee.Id);
            _manager.AddInParameter("@FirstName", DbType.String, employee.FirstName);
            _manager.AddInParameter("@LastName", DbType.String, employee.LastName);
            _manager.AddInParameter("@NickName", DbType.String, employee.NickName);
            _manager.AddInParameter("@RoleId", DbType.Int32, employee.RoleId);
            _manager.AddInParameter("@DOB", DbType.DateTime, employee.DOB);

            _manager.ExecuteNonQuery();

        }


        #endregion


    }
}
